package com.sunmi.reader.demo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sunmi.reader.usbhid.SunmiReader;
import com.sunmi.reader.usbhid.TrackData;

import java.util.Arrays;
import java.util.Date;
import java.util.Random;

import static android.os.SystemClock.sleep;


/**
 * Created by Administrator on 2017/4/10.
 */
public class CardControlActivity extends Activity {

    private byte[] type;
    private byte[] sv;
    private byte[] hv;
    private static byte[] iso14443auid;
    private static byte[] iso15693uid;
    private static byte[] felicauid;
    private static byte[] pupi;
    private static byte[] appField;
    private static byte[] protocol;
    private byte sle4442tag;
    private byte sle4428tag;
    private byte at24cxxtag;
    private byte at88sc102tag;
    private byte at88sc1604tag;
    private byte at45dbtag;
    private byte stripcheckcardoutime = 0;
    private byte stripcheckcardoutimetag;
    private String texttemp;
    private String data;
    private String rand;
    private String writedata;
    private static boolean auto_scanf_card_flag = false;
    private static boolean manual_scanf_card_flag = false;
    private static boolean auto_scanf_card_thread_flag = false;
    private static boolean manual_scanf_card_thread_flag = false;
    private char[] totaltext;
    private char[] totaltextbak;
    private char[] totaltexterror;
    private char[] totaltextlog;
    private char tag;
    private int totallen;
    private int totallenbak;
    private int totallenerror;
    private int totallenlog;
    private int ret;
    private TextView auto_scanf_card;
    private TextView auto_scanf_card_explain;
    private TextView manual_scanf_card;
    private TextView scanf_card_info_textview;
    private ScrollView scanf_card_info_scrollview;
    private Button scanf_card_button;
    private static char auto_scanf_card_view = 0;
    private static char manual_scanf_card_view = 0;
    private RelativeLayout manual_scanf_card_explain;
    public static SunmiReader sunmireader = new SunmiReader();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                default:
                    texttemp = "\n寻找磁条卡等待超时时间······"+msg.what;
                    System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
                    totallenlog += texttemp.length();

                    DisplayShow(totaltextlog, totallenlog);
                    break;
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            manual_scanf_card_view = 0;
            auto_scanf_card_view = 0;
            auto_scanf_card_flag = false;
            manual_scanf_card_flag = false;

            while (manual_scanf_card_thread_flag) {
                sleep(10);
            }

            while (auto_scanf_card_thread_flag) {
                sleep(10);
            }

            Arrays.fill(iso14443auid, (byte) 0);
            Arrays.fill(protocol, (byte) 0);
            Arrays.fill(appField, (byte) 0);
            Arrays.fill(pupi, (byte) 0);
            Arrays.fill(felicauid, (byte) 0);
            Arrays.fill(iso15693uid, (byte) 0);
        }

        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_control);

        felicauid = new byte[16];
        pupi = new byte[4];
        appField = new byte[4];
        protocol = new byte[4];
        iso14443auid = new byte[10];
        iso15693uid = new byte[8];
        totaltext = new char[1024];
        totaltextbak = new char[1024];
        totaltextlog = new char[1024];
        totaltexterror = new char[1024];
        type = new byte[50];
        sv = new byte[50];
        hv = new byte[50];

        scanf_card_info_textview = (TextView) findViewById(R.id.SCANF_CARD_INFO_TEXTVIEW);
        scanf_card_info_scrollview = (ScrollView) findViewById(R.id.SCANF_CARD_INFO_SCROLLVIEW);

        auto_scanf_card = (TextView) findViewById(R.id.AUTO_SCANF_CARD);
        auto_scanf_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (manual_scanf_card_view == 1) {
                    manual_scanf_card_flag = false;
                    manual_scanf_card_view = 0;

                    manual_scanf_card.setTextColor(Color.parseColor("#C9D3FF"));
                    manual_scanf_card.setBackgroundColor(Color.parseColor("#3E476E"));

                    Drawable drawable = getResources().getDrawable(R.drawable.unselect);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    manual_scanf_card.setCompoundDrawables(null, null, drawable, null);

                    manual_scanf_card_explain = (RelativeLayout) findViewById(R.id.MANUAL_SCANF_CARD_EXPLAIN);
                    manual_scanf_card_explain.setVisibility(View.GONE);
                }

                if (auto_scanf_card_view == 0) {
                    if (auto_scanf_card_thread_flag) return;
                    if (auto_scanf_card_flag) return;

                    auto_scanf_card_flag = true;
                    auto_scanf_card_thread_flag = true;

                    auto_scanf_card_view = 1;

                    Arrays.fill(iso14443auid, (byte) 0);
                    Arrays.fill(protocol, (byte) 0);
                    Arrays.fill(appField, (byte) 0);
                    Arrays.fill(pupi, (byte) 0);
                    Arrays.fill(felicauid, (byte) 0);
                    Arrays.fill(iso15693uid, (byte) 0);

                    auto_scanf_card.setTextColor(Color.parseColor("#3E476E"));
                    auto_scanf_card.setBackgroundColor(Color.parseColor("#C9D1F5"));

                    Drawable drawable = getResources().getDrawable(R.drawable.select);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    auto_scanf_card.setCompoundDrawables(null, null, drawable, null);

                    auto_scanf_card_explain = (TextView) findViewById(R.id.AUTO_SCANF_CARD_EXPLAIN);
                    auto_scanf_card_explain.setVisibility(View.VISIBLE);

                    manual_scanf_card = (TextView) findViewById(R.id.MANUAL_SCANF_CARD);
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) manual_scanf_card.getLayoutParams();
                    params.addRule(RelativeLayout.BELOW, auto_scanf_card_explain.getId());
                    manual_scanf_card.setLayoutParams(params);

                    while (manual_scanf_card_thread_flag) {
                        sleep(10);
                    }

                    scanf_card_info_textview.setText(" ");

                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            tag = 0;
                            totallen = 0;
                            totallenbak = 0;
                            totallenlog = 0;

                            if (!sunmireader.isOpen()) {
                                ret = sunmireader.open(getApplicationContext());
                                if (ret < -1) {
                                    texttemp = "1. 未检测到读写器";
                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                    totallen += texttemp.length();

                                    DisplayShow(totaltext, totallen);

                                    auto_scanf_card_flag = false;
                                    auto_scanf_card_thread_flag = false;
                                    return;
                                }
                            }

                            texttemp = "1. 成功打开读写器\n";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            ret = sunmireader.getReaderType(type);
                            if (ret < 0) {
                                texttemp = "获取读写器型号失败";
                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                totallen += texttemp.length();

                                DisplayShow(totaltext, totallen);

                                sunmireader.close();
                                auto_scanf_card_flag = false;
                                auto_scanf_card_thread_flag = false;
                                return;
                            }

                            texttemp = "读写器型号: ";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            texttemp = BytestoASCII(type, ret);
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            texttemp = ";";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            ret = sunmireader.getReaderSoftwareVersion(sv);
                            if (ret < 0) {
                                texttemp = "获取读写器软件版本失败";
                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                totallen += texttemp.length();

                                DisplayShow(totaltext, totallen);

                                sunmireader.close();
                                auto_scanf_card_flag = false;
                                auto_scanf_card_thread_flag = false;
                                return;
                            }

                            texttemp = "软件版本: ";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            texttemp = BytestoASCII(sv, ret);
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            texttemp = ";";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            ret = sunmireader.getReaderHardwareVersion(hv);
                            if (ret < 0) {
                                texttemp = "获取读写器硬件版本失败";
                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                totallen += texttemp.length();

                                DisplayShow(totaltext, totallen);

                                sunmireader.close();
                                auto_scanf_card_flag = false;
                                auto_scanf_card_thread_flag = false;
                                return;
                            }

                            texttemp = "硬件版本: ";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            texttemp = BytestoASCII(hv, ret);
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            texttemp = "\n";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            totallenbak = totallen;
                            System.arraycopy(totaltext, 0, totaltextbak, 0, totallen);

                            texttemp = "读取中...\n";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
                            totallenerror += texttemp.length();

                            DisplayShow(totaltexterror, totallenerror);

                            sle4442tag = 0;
                            sle4428tag = 0;
                            at24cxxtag = 0;
                            at88sc102tag = 0;
                            at88sc1604tag = 0;
                            at45dbtag = 0;

                            while (auto_scanf_card_flag) {

                                if (!sunmireader.isOpen()) {
                                    auto_scanf_card_flag = false;
                                    continue;
                                }

                                totallenlog = totallenerror;
                                totallen = totallenbak;
                                totallenerror = 0;

                                System.arraycopy(totaltextbak, 0, totaltext, 0, totallen);
                                System.arraycopy(totaltexterror, 0, totaltextlog, 0, totallenlog);

                                ret = ISO14443ACheckCard();
                                if (ret == 0) continue;

                                ret = ISO14443BCheckCard();
                                if (ret == 0) continue;

                                ret = FelicaCheckCard();
                                if (ret == 0) continue;

                                ret = ISO15693CheckCard();
                                if (ret == 0) continue;

                                ret = SAMCheckCard();
                                if (ret == 0) continue;

                                ret = CheckCardExist();
                                if (ret == 0) {
                                    ret = SLE4442CheckCard();
                                    if (ret == 0) continue;

                                    ret = SLE4428CheckCard();
                                    if (ret == 0) continue;

                                    ret = AT88SC1604CheckCard();
                                    if (ret == 0) continue;

                                    ret = AT88SC102CheckCard();
                                    if (ret == 0) continue;

                                    ret = AT45DBCheckCard();
                                    if (ret == 0) continue;

                                    ret = AT24CXXCheckCard();
                                    if (ret == 0) continue;

                                    ret = CPUCheckCard();
                                    if (ret == 0) continue;
                                }
                                else if (ret == 1) {
                                    continue;
                                }

                                if (type[0] == 'T' && type[1] == '1' && type[2] == '0') {
                                    ret = StripCheckCard();
                                    if (ret == 0) continue;
                                }

                                Arrays.fill(iso14443auid, (byte) 0);
                                Arrays.fill(protocol, (byte) 0);
                                Arrays.fill(appField, (byte) 0);
                                Arrays.fill(pupi, (byte) 0);
                                Arrays.fill(felicauid, (byte) 0);
                                Arrays.fill(iso15693uid, (byte) 0);

                                if (auto_scanf_card_flag) {
                                    texttemp = "\n读取中...\n";
                                    System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
                                    totallenerror += texttemp.length();
                                } else {
                                    texttemp = "  ";

                                    DisplayShow(texttemp.toCharArray(), texttemp.length());
                                }
                            }

                            texttemp = "  ";

                            DisplayShow(texttemp.toCharArray(), texttemp.length());

                            auto_scanf_card_thread_flag = false;
                            sunmireader.close();
                        }
                    });

                    thread.start();
                } else {
                    auto_scanf_card_view = 0;
                    auto_scanf_card_flag = false;

                    scanf_card_info_textview.setText(" ");

                    auto_scanf_card.setTextColor(Color.parseColor("#C9D3FF"));
                    auto_scanf_card.setBackgroundColor(Color.parseColor("#3E476E"));

                    Drawable drawable = getResources().getDrawable(R.drawable.unselect);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    auto_scanf_card.setCompoundDrawables(null, null, drawable, null);

                    auto_scanf_card_explain = (TextView) findViewById(R.id.AUTO_SCANF_CARD_EXPLAIN);
                    auto_scanf_card_explain.setVisibility(View.GONE);

                    manual_scanf_card = (TextView) findViewById(R.id.MANUAL_SCANF_CARD);
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) manual_scanf_card.getLayoutParams();
                    params.addRule(RelativeLayout.BELOW, auto_scanf_card.getId());
                    manual_scanf_card.setLayoutParams(params);
                }
            }
        });

        manual_scanf_card = (TextView) findViewById(R.id.MANUAL_SCANF_CARD);
        manual_scanf_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (auto_scanf_card_view == 1) {
                    auto_scanf_card_flag = false;
                    auto_scanf_card_view = 0;

                    auto_scanf_card.setTextColor(Color.parseColor("#C9D3FF"));
                    auto_scanf_card.setBackgroundColor(Color.parseColor("#3E476E"));

                    Drawable drawable = getResources().getDrawable(R.drawable.unselect);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    auto_scanf_card.setCompoundDrawables(null, null, drawable, null);

                    auto_scanf_card_explain = (TextView) findViewById(R.id.AUTO_SCANF_CARD_EXPLAIN);
                    auto_scanf_card_explain.setVisibility(View.GONE);

                    manual_scanf_card = (TextView) findViewById(R.id.MANUAL_SCANF_CARD);
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) manual_scanf_card.getLayoutParams();
                    params.addRule(RelativeLayout.BELOW, auto_scanf_card.getId());
                    manual_scanf_card.setLayoutParams(params);
                }

                if (manual_scanf_card_view == 0) {
                    manual_scanf_card_view = 1;

                    manual_scanf_card.setTextColor(Color.parseColor("#3E476E"));
                    manual_scanf_card.setBackgroundColor(Color.parseColor("#C9D1F5"));

                    Drawable drawable = getResources().getDrawable(R.drawable.select);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    manual_scanf_card.setCompoundDrawables(null, null, drawable, null);

                    manual_scanf_card_explain = (RelativeLayout) findViewById(R.id.MANUAL_SCANF_CARD_EXPLAIN);
                    manual_scanf_card_explain.setVisibility(View.VISIBLE);
                } else {
                    manual_scanf_card_view = 0;
                    manual_scanf_card_flag = false;

                    scanf_card_info_textview.setText(" ");

                    manual_scanf_card.setTextColor(Color.parseColor("#C9D3FF"));
                    manual_scanf_card.setBackgroundColor(Color.parseColor("#3E476E"));

                    Drawable drawable = getResources().getDrawable(R.drawable.unselect);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    manual_scanf_card.setCompoundDrawables(null, null, drawable, null);

                    manual_scanf_card_explain = (RelativeLayout) findViewById(R.id.MANUAL_SCANF_CARD_EXPLAIN);
                    manual_scanf_card_explain.setVisibility(View.GONE);
                }
            }
        });

        scanf_card_button = (Button) findViewById(R.id.SCANF_CARD_BUTTON);
        scanf_card_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                while (auto_scanf_card_thread_flag) {
                    sleep(10);
                }

                if (manual_scanf_card_thread_flag) return;
                if (manual_scanf_card_flag) return;

                scanf_card_info_textview.setText(" ");

                manual_scanf_card_flag = true;
                manual_scanf_card_thread_flag = true;

                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        tag = 0;
                        totallen = 0;
                        totallenbak = 0;
                        totallenlog = 0;

                        if (!sunmireader.isOpen()) {
                            ret = sunmireader.open(getApplicationContext());
                            if (ret < -1) {
                                texttemp = "1. 未检测到读写器";
                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                totallen += texttemp.length();

                                DisplayShow(totaltext, totallen);

                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                return;
                            }
                        }

                        Arrays.fill(iso14443auid, (byte) 0);
                        Arrays.fill(protocol, (byte) 0);
                        Arrays.fill(appField, (byte) 0);
                        Arrays.fill(pupi, (byte) 0);
                        Arrays.fill(felicauid, (byte) 0);
                        Arrays.fill(iso15693uid, (byte) 0);

                        texttemp = "1. 成功打开读写器\n";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        ret = sunmireader.getReaderType(type);
                        if (ret < 0) {
                            texttemp = "获取读写器型号失败";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            DisplayShow(totaltext, totallen);

                            sunmireader.close();
                            auto_scanf_card_flag = false;
                            auto_scanf_card_thread_flag = false;
                            return;
                        }

                        texttemp = "读写器型号: ";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = BytestoASCII(type, ret);
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = ";";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        ret = sunmireader.getReaderSoftwareVersion(sv);
                        if (ret < 0) {
                            texttemp = "获取读写器软件版本失败";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            DisplayShow(totaltext, totallen);

                            sunmireader.close();
                            auto_scanf_card_flag = false;
                            auto_scanf_card_thread_flag = false;
                            return;
                        }

                        texttemp = "软件版本: ";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = BytestoASCII(sv, ret);
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = ";";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        ret = sunmireader.getReaderHardwareVersion(hv);
                        if (ret < 0) {
                            texttemp = "获取读写器硬件版本失败";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                            totallen += texttemp.length();

                            DisplayShow(totaltext, totallen);

                            sunmireader.close();
                            auto_scanf_card_flag = false;
                            auto_scanf_card_thread_flag = false;
                            return;
                        }

                        texttemp = "硬件版本: ";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = BytestoASCII(hv, ret);
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = "\n";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        totallenbak = totallen;
                        System.arraycopy(totaltext, 0, totaltextbak, 0, totallen);

                        texttemp = "读取中...\n";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
                        totallenerror += texttemp.length();

                        DisplayShow(totaltexterror, totallenerror);
                        
                        sle4442tag = 0;
                        sle4428tag = 0;
                        at24cxxtag = 0;
                        at88sc102tag = 0;
                        at88sc1604tag = 0;
                        at45dbtag = 0;

                        Date dateold = new Date();
                        Long timeold = dateold.getTime();
                        Long timenew = timeold;

                        while ((timenew - timeold) / 1000 < 10) {

                            if (!sunmireader.isOpen()) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            if (!manual_scanf_card_flag) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            totallenlog = totallenerror;
                            totallen = totallenbak;
                            totallenerror = 0;

                            System.arraycopy(totaltextbak, 0, totaltext, 0, totallen);
                            System.arraycopy(totaltexterror, 0, totaltextlog, 0, totallenlog);

                            ret = ISO14443ACheckCard();
                            if (ret == 0) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            ret = ISO14443BCheckCard();
                            if (ret == 0) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            ret = FelicaCheckCard();
                            if (ret == 0) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            ret = ISO15693CheckCard();
                            if (ret == 0) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            ret = SAMCheckCard();
                            if (ret == 0) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            ret = CheckCardExist();
                            if (ret == 0) {
                                ret = SLE4442CheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }

                                ret = SLE4428CheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }

                                ret = AT88SC1604CheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }

                                ret = AT88SC102CheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }

                                ret = AT45DBCheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }

                                ret = AT24CXXCheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }

                                ret = CPUCheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }
                            }
                            else if (ret == 1) {
                                manual_scanf_card_flag = false;
                                manual_scanf_card_thread_flag = false;
                                sunmireader.close();
                                return;
                            }

                            if (type[0] == 'T' && type[1] == '1' && type[2] == '0') {
                                ret = StripCheckCard();
                                if (ret == 0) {
                                    manual_scanf_card_flag = false;
                                    manual_scanf_card_thread_flag = false;
                                    sunmireader.close();
                                    return;
                                }
                            }

                            texttemp = "\n读取中...\n";
                            System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
                            totallenerror += texttemp.length();

                            Date datenew = new Date();
                            timenew = datenew.getTime();
                        }

                        texttemp = "\n读取中...\n";
                        totallenerror -= texttemp.length();

                        texttemp = "\n读取失败，超时...";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
                        totallenerror += texttemp.length();

                        DisplayShow(totaltexterror, totallenerror);

                        manual_scanf_card_flag = false;
                        manual_scanf_card_thread_flag = false;
                        sunmireader.close();
                        return;
                    }
                });

                thread.start();
            }
        });
    }

    public int ISO14443ACheckCard() {

        byte[] type = new byte[2];
        byte[] uid = new byte[10];

        if (this.type[0] == 'R' && this.type[1] == '3' && this.type[2] == '2' && this.type[3] == '1'  && this.type[4] == 'S'  && this.type[5] == 'T') {
            for (int i = 0; i < 3; ++i) {
                ret = sunmireader.iso14443ACheckCard(true, uid, type);
                if (ret < 0) continue;
                break;
            }
        } else {
            ret = sunmireader.iso14443ACheckCard(false, uid, type);
        }

        if (ret > 0) {
            if (Arrays.equals(iso14443auid, uid)) return 0;
            System.arraycopy(uid, 0, iso14443auid, 0, iso14443auid.length);

            sunmireader.beepTime((char) 100);

            texttemp = "\n开始寻找非接ISO14443A的卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);

            if (type[0] == 0x04 && type[1] == 0x00) {
                texttemp = "2. 寻找到UID为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(uid, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "的卡片\n卡片协议类型：ISO14443A，卡片类型：M1 S50卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取块数据：\n读取块地址为0x01，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] key = HexStringtoBytes("FFFFFFFFFFFF");

                ret = sunmireader.iso14443AM1Auth((byte) 0, (byte) 0x01, key);
                if (ret < 0) {
                    texttemp = "认证失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                byte[] readblock = new byte[16];

                ret = sunmireader.iso14443AM1ReadBlock((byte) 0x01, readblock);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(readblock, readblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始写入块数据：\n写入块地址为0x01，写入数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                data = "20170615112233445566778899000000";
                rand = String.valueOf(Rand());

                writedata = data.substring(0, data.length() - rand.length());
                writedata += rand;
                byte[] writeblock = HexStringtoBytes(writedata);

                texttemp = BytestoHexString(writeblock, writeblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.iso14443AM1WriteBlock((byte) 0x01, writeblock);
                if (ret < 0) {
                    texttemp = "，写入数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "，写入数据成功\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取块数据：\n读取块地址为0x01，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.iso14443AM1ReadBlock((byte) 0x01, readblock);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(readblock, readblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            } else if (type[0] == 0x02) {
                texttemp = "2. 寻找到UID为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(uid, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "的卡片\n卡片协议类型：ISO14443A，卡片类型：M1 S70卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取块数据：\n读取块地址为0x01，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] key = HexStringtoBytes("FFFFFFFFFFFF");

                ret = sunmireader.iso14443AM1Auth((byte) 0, (byte) 0x01, key);
                if (ret < 0) {
                    texttemp = "认证失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                byte[] readblock = new byte[16];

                ret = sunmireader.iso14443AM1ReadBlock((byte) 0x01, readblock);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(readblock, readblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始写入块数据：\n写入块地址为0x01，写入数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                data = "20170615112233445566778899000000";
                rand = String.valueOf(Rand());

                writedata = data.substring(0, data.length() - rand.length());
                writedata += rand;
                byte[] writeblock = HexStringtoBytes(writedata);

                texttemp = BytestoHexString(writeblock, writeblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.iso14443AM1WriteBlock((byte) 0x01, writeblock);
                if (ret < 0) {
                    texttemp = "，写入数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "，写入数据成功\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取块数据：\n读取块地址为0x01，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.iso14443AM1ReadBlock((byte) 0x01, readblock);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(readblock, readblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            } else if (type[0] == 0x08) {
                texttemp = "2. 寻找到UID为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(uid, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "的卡片\n卡片协议类型：ISO14443A，卡片类型：Pro CPU卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始RATS：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] rats = new byte[255];

                ret = sunmireader.iso14443ARats(rats);
                if (ret < 0) {
                    texttemp = "rats失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "rats成功：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(rats, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始APDU透传数据：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] cmd = HexStringtoBytes("00A40000023F00");

                texttemp = "APDU发送：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(cmd, cmd.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] rsp = new byte[1024];

                ret = sunmireader.iso14443AAPDU(cmd, (byte) cmd.length, rsp);
                if (ret < 0) {
                    texttemp = "\nAPDU发送失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "\nAPDU接收：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(rsp, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            } else if (type[0] == 0x04 && type[1] == 0x03) {
                texttemp = "2. 寻找到UID为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(uid, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "的卡片\n卡片协议类型：ISO14443A，卡片类型：ProX CPU卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始RATS：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] rats = new byte[255];

                ret = sunmireader.iso14443ARats(rats);
                if (ret < 0) {
                    texttemp = "rats失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "rats成功：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(rats, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始APDU透传数据：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] cmd = HexStringtoBytes("00A40000023F00");

                texttemp = "APDU发送：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(cmd, cmd.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] rsp = new byte[1024];

                ret = sunmireader.iso14443AAPDU(cmd, (byte) cmd.length, rsp);
                if (ret < 0) {
                    texttemp = "\nAPDU发送失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "\nAPDU接收：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(rsp, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            } else if (type[0] == 0x44 && type[1] == 0x03) {
                texttemp = "2. 寻找到UID为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(uid, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "的卡片\n卡片协议类型：ISO14443A，卡片类型：DESFire CPU卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始RATS：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] rats = new byte[255];

                ret = sunmireader.iso14443ARats(rats);
                if (ret < 0) {
                    texttemp = "rats失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "rats成功：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(rats, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始APDU透传数据：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] cmd = HexStringtoBytes("00A40000023F00");

                texttemp = "APDU发送：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(cmd, cmd.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] rsp = new byte[1024];

                ret = sunmireader.iso14443AAPDU(cmd, (byte) cmd.length, rsp);
                if (ret < 0) {
                    texttemp = "\nAPDU发送失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "\nAPDU接收：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(rsp, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            } else if (type[0] == 0x44 && type[1] == 0x00) {
                texttemp = "2. 寻找到UID为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(uid, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "的卡片\n卡片协议类型：ISO14443A，卡片类型：UltraLight卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取块数据：\n读取块首地址为0x05，块数目：1，\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] readblock = new byte[4];

                ret = sunmireader.iso14443AM0ReadBlock((byte) 0x05, readblock);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(readblock, readblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始写入块数据：\n写入块首地址为0x05，块数目：1，写入数据为：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte[] writeblock = new byte[4];

                data = "11223344";
                rand = String.valueOf(Rand());

                if (rand.length() < data.length()) {
                    writedata = data.substring(0, data.length() - rand.length());
                    writedata += rand;
                } else {
                    writedata = rand.substring(0, data.length());
                }

                data = writedata;

                System.arraycopy(HexStringtoBytes(data), 0, writeblock, 0, writeblock.length);

                texttemp = BytestoHexString(writeblock, writeblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.iso14443AM0WriteBlock((byte) 0x05, writeblock);
                if (ret < 0) {
                    texttemp = "，写入数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "，写入数据成功\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取块数据：\n读取块首地址为0x05，块数目：1，\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.iso14443AM0ReadBlock((byte) 0x05, readblock);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x01;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
                
                texttemp = BytestoHexString(readblock, readblock.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            } else {
                texttemp = "卡片不符合iso14443A协议";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                DisplayShow(totaltext, totallen);

                tag |= 0x01;

                return 0;
            }

            DisplayShow(totaltext, totallen);

            sunmireader.iso14443AHalt();

            tag |= 0x01;

            return 0;
        }
        else {
            texttemp = "\n开始寻找非接ISO14443A的卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);
        }

        tag &= 0xFFFE;

        texttemp = "\n未寻找到非接ISO14443A的卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int ISO14443BCheckCard() {

        byte[] pupi2 = new byte[4];
        byte[] appField2 = new byte[4];
        byte[] protocol2 = new byte[4];

        if (this.type[0] == 'R' && this.type[1] == '3' && this.type[2] == '2' && this.type[3] == '1'  && this.type[4] == 'S'  && this.type[5] == 'T') {
            for (int i = 0; i < 3; ++i) {
                ret = sunmireader.iso14443BCheckCard((byte) 0x00, pupi2, appField2, protocol2);
                if (ret < 0) continue;
                break;
            }
        } else {
            ret = sunmireader.iso14443BCheckCard((byte) 0x00, pupi2, appField2, protocol2);
        }

        if (ret == -7) {
            texttemp = "读写器不支持ISO14443B协议\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
            totallenerror += texttemp.length();

            return -1;
        }
        else if (ret == 0) {
            if (Arrays.equals(appField, appField2) && Arrays.equals(pupi, pupi2) && Arrays.equals(protocol, protocol2)) {
                if (pupi[0] != 0x00 || pupi[1] != 0x00 || pupi[2] != 0x00 || pupi[3] != 0x00)
                    return 0;
                if (appField[0] != 0x00 || appField[1] != 0x00 || appField[2] != 0x00 || appField[3] != 0x00)
                    return 0;
                if (protocol[0] != 0x00 || protocol[1] != 0x00 || protocol[2] != 0x00 || protocol[3] != 0x00)
                    return 0;
            }

            System.arraycopy(appField2, 0, appField, 0, appField.length);
            System.arraycopy(pupi2, 0, pupi, 0, pupi.length);
            System.arraycopy(protocol2, 0, protocol, 0, protocol.length);

            sunmireader.beepTime((char) 100);

            texttemp = "\n开始寻找非接ISO14443B的卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);

            texttemp = "2. 寻找到PUPI为";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(pupi2, pupi2.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "，应用信息为";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(appField2, appField2.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "，协议信息为";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(protocol2, protocol2.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "的卡片\n卡片协议类型：ISO14443B\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "3. 开始操作卡片：\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "开始APDU透传数据：\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            byte[] cmd = HexStringtoBytes("05000000");

            texttemp = "APDU发送：";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(cmd, cmd.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            byte[] rsp = new byte[1024];

            if (type[0] == 'R' && type[1] == '3' && type[2] == '2' && type[3] == '1'  && type[4] == 'S'  && type[5] == 'T') {
                ret = sunmireader.iso14443BDtu(cmd, (byte) cmd.length, rsp);
                if (ret < 0) {
                    if (ret == -7) {
                        texttemp = "\n读写器不支持该指令";
                    } else {
                        texttemp = "\nDTU发送失败";
                    }

                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x02;

                    return 0;
                }
            } else {
                ret = sunmireader.iso14443BAPDU(cmd, (byte) cmd.length, rsp);
                if (ret < 0) {
                    if (ret == -7) {
                        texttemp = "\n读写器不支持该指令";
                    } else {
                        texttemp = "\nAPDU发送失败";
                    }

                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x02;

                    return 0;
                }
            }

            texttemp = "\nAPDU接收：";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(rsp, ret);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            DisplayShow(totaltext, totallen);

            sunmireader.iso14443BHalt();

            tag |= 0x02;

            return 0;
        }
        else {
            texttemp = "\n开始寻找非接ISO14443B的卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);
        }

        tag &= 0xFFFD;

        texttemp = "\n未寻找到非接ISO14443B的卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int FelicaCheckCard() {

        byte [] uid = new byte[16];

        if (this.type[0] == 'R' && this.type[1] == '3' && this.type[2] == '2' && this.type[3] == '1'  && this.type[4] == 'S'  && this.type[5] == 'T') {
            for (int i = 0; i < 3; ++i) {
                ret = sunmireader.felicaCheckCard((byte) 0x00, uid);
                if (ret < 0) continue;
                break;
            }
        } else {
            ret = sunmireader.felicaCheckCard((byte) 0x00, uid);
        }

        if (ret == -7) {
            texttemp = "读写器不支持ISO18092协议\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
            totallenerror += texttemp.length();

            return -1;
        }
        else if (ret == 0) {
            if (Arrays.equals(felicauid, uid)) return 0;

            System.arraycopy(uid, 0, felicauid, 0, felicauid.length);

            sunmireader.beepTime((char) 100);

            texttemp = "\n开始寻找非接Felica卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);

            texttemp = "2. 寻找到UID为";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(uid, uid.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "的卡片\n卡片协议类型：ISO18092，卡片类型：Felica卡\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "3. 开始操作卡片：\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "开始透传用户帧数据：\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            byte [] cmd = new byte[256];
            byte [] rsp = new byte[1024];

            String cmd2 = "00FFFF0000";
            byte cmdlen = (byte) (cmd2.length() / 2);
            System.arraycopy(HexStringtoBytes(cmd2), 0, cmd, 0, cmdlen);

            texttemp = "透传用户帧数据发送：";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(cmd, cmdlen);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            ret = sunmireader.felicaDtu(cmd, cmdlen, rsp);
            if (ret < 0) {
                if (ret == -7) {
                    texttemp = "\n读写器不支持ISO18092协议";
                } else {
                    texttemp = "\n透传用户帧数据失败";
                }

                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                DisplayShow(totaltext, totallen);

                tag |= 0x04;

                return 0;
            }

            texttemp = "\n透传用户帧数据接收：";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(rsp, ret);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            DisplayShow(totaltext, totallen);

            tag |= 0x04;

            return 0;
        }
        else {
            texttemp = "\n开始寻找非接Felica卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);
        }

        tag &= 0xFFFB;

        texttemp = "\n未寻找到非接Felica卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int ISO15693CheckCard() {

        byte [] uid = new byte[8];

        if (this.type[0] == 'R' && this.type[1] == '3' && this.type[2] == '2' && this.type[3] == '1'  && this.type[4] == 'S'  && this.type[5] == 'T') {
            for (int i = 0; i < 3; ++i) {
                ret = sunmireader.iso15693CheckCard(false, uid);
                if (ret < 0) continue;
                break;
            }
        } else {
            ret = sunmireader.iso15693CheckCard(false, uid);
        }

        if (ret == -7) {
            texttemp = "读写器不支持ISO15693协议\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltexterror, totallenerror, texttemp.length());
            totallenerror += texttemp.length();

            return -1;
        }
        else if (ret == 0) {
            if (Arrays.equals(iso15693uid, uid)) return 0;

            System.arraycopy(uid, 0, iso15693uid, 0, iso15693uid.length);

            sunmireader.beepTime((char) 100);

            texttemp = "\n开始寻找非接ISO15693的卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);

            texttemp = "2. 寻找到UID为";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(uid, uid.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "的卡片\n卡片协议类型：ISO15693\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "3. 开始操作卡片：\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "开始读取块数据：\n读取块首地址为0x01，块数目：1，\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            byte [] readblock = new byte[4];

            ret = sunmireader.iso15693ReadBlock(uid, (byte) 0x01, readblock);
            if (ret < 0) {
                texttemp = "读取数据失败";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                DisplayShow(totaltext, totallen);

                tag |= 0x08;

                return 0;
            }

            texttemp = "读取数据成功，数据为";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(readblock, readblock.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "\n开始写入块数据：\n写入块首地址为0x01，块数目：1，写入数据为：";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            byte [] writeblock = new byte[4];

            data = "01020304";
            rand = String.valueOf(Rand());

            if (rand.length() < data.length()) {
                writedata = data.substring(0, data.length() - rand.length());
                writedata += rand;
            } else {
                writedata = rand.substring(0, data.length());
            }

            data = writedata;

            System.arraycopy(HexStringtoBytes(data), 0, writeblock, 0, writeblock.length);

            texttemp = BytestoHexString(writeblock, writeblock.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            ret = sunmireader.iso15693WriteBlock(uid, (byte) 0x01, writeblock);
            if (ret < 0) {
                texttemp = "，写入数据失败";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                DisplayShow(totaltext, totallen);

                tag |= 0x08;

                return 0;
            }

            texttemp = "，写入数据成功\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "开始读取块数据：\n读取块首地址为0x01，块数目：1，\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            ret = sunmireader.iso15693ReadBlock(uid, (byte) 0x01, readblock);
            if (ret < 0) {
                texttemp = "读取数据失败";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                DisplayShow(totaltext, totallen);

                tag |= 0x08;

                return 0;
            }

            texttemp = "读取数据成功，数据为";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(readblock, readblock.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            DisplayShow(totaltext, totallen);

            tag |= 0x08;

            return 0;
        }
        else {
            texttemp = "\n开始寻找非接ISO15693的卡";
            System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
            totallenlog += texttemp.length();

            DisplayShow(totaltextlog, totallenlog);
        }

        tag &= 0xFFF7;

        texttemp = "\n未寻找到非接ISO15693的卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int SAMCheckCard() {

        byte tag2 = 0;
        byte index = 0;

        if (type[0] == 'R' && type[1] == '3' && type[2] == '2' && type[3] == '1'  && type[4] == 'S'  && type[5] == 'T') {
            return -1;
        }

        texttemp = "\n开始寻找SAM卡槽SAM卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        for (int n = 0; n < 4; ++n) {
            index = (byte) (n + 2);

            tag2 = 0;

            ret = sunmireader.cardSlotCheck(index);
            if (ret == 0 || ret == -7) {
                byte [] atr = new byte[255];

                ret = sunmireader.cardSlotControl(index, (byte) 0x01, (byte) 0x00, atr);
                if (ret > 0) {
                    texttemp = "2. 寻找到接触式SAM卡\n";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "3. 开始操作卡片：\n卡片上电成功\nATR : ";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = BytestoHexString(atr, ret);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    byte [] rsp = new byte[1024];

                    byte [] cmd = HexStringtoBytes("00A40000023F00");

                    texttemp = "\nAPDU发送：";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = BytestoHexString(cmd, cmd.length);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    ret = sunmireader.iso7816APDU(index, cmd, (byte) cmd.length, rsp);
                    if (ret >= 0) {
                        texttemp = "\nAPDU接收：";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = BytestoHexString(rsp, ret);
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag2 = 1;
                        break;
                    } else {
                        texttemp = "\nAPDU发送失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag2 = 1;
                        break;
                    }
                }
            }
        }

        if (tag2 == 1) {
            tag |= 0x10;
            return 0;
        }

        tag &= 0xFFEF;

        texttemp = "\n未寻找到SAM卡槽SAM卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int CPUCheckCard() {

        byte index = 0x00;

        texttemp = "\n开始寻找副卡槽CPU卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        byte [] atr = new byte[255];

        ret = sunmireader.cardSlotControl(index, (byte) 0x01, (byte) 0x00, atr);
        if (ret > 0) {
            texttemp = "2. 寻找到接触式CPU卡\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "3. 开始操作卡片：\n卡片上电成功\nATR : ";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(atr, ret);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            byte [] rsp = new byte[1024];

            byte [] cmd = HexStringtoBytes("00A40000023F00");

            texttemp = "\nAPDU发送：";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = BytestoHexString(cmd, cmd.length);
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            ret = sunmireader.iso7816APDU(index, cmd, (byte) cmd.length, rsp);
            if (ret >= 0) {
                texttemp = "\nAPDU接收：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(rsp, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                DisplayShow(totaltext, totallen);

                tag |= 0x20;

                return 0;
            } else {
                texttemp = "\nAPDU发送失败";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                DisplayShow(totaltext, totallen);

                tag |= 0x20;

                return 0;
            }
        }

        texttemp = "\n未寻找到副卡槽CPU卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int SLE4442CheckCard() {

        texttemp = "\n开始寻找副卡槽SLE4442卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        if (sle4442tag == 1) {
            return 0;
        } else if (sle4442tag == 0) {
            ret = sunmireader.sle4432TO5542Select();
            if (ret == 0) {
                byte [] key = HexStringtoBytes("FFFFFF");

                ret = sunmireader.sle4442And5542Auth(key);
                if (ret == 0) {
                    texttemp = "2. 寻找到接触式SLE4442卡\n";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    byte len = 16;
                    byte [] readdata = new byte[len];

                    ret = sunmireader.sle4432TO5542ReadData((byte) 0x00, len, readdata);
                    if (ret < 0) {
                        texttemp = "读取数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x40;

                        return 0;
                    }

                    texttemp = "读取数据成功，数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = BytestoHexString(readdata, ret);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    data = "20170615112233445566778899000000";
                    rand = String.valueOf(Rand());

                    writedata = data.substring(0, data.length() - rand.length());
                    writedata += rand;
                    byte [] writedata2 = HexStringtoBytes(writedata);

                    texttemp = BytestoHexString(writedata2, writedata2.length);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    ret = sunmireader.sle4432TO5542WriteData((byte) 0x00, (byte) writedata2.length, writedata2);
                    if (ret < 0) {
                        texttemp = "，写入数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x40;

                        return 0;
                    }

                    texttemp = "，写入数据成功\n";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "开始读取数据：\n读取地址为0x00，";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    len = 16;

                    ret = sunmireader.sle4432TO5542ReadData((byte) 0x00, len, readdata);
                    if (ret < 0) {
                        texttemp = "读取数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x40;

                        return 0;
                    }

                    int k;

                    for (k = 0; k < len; ++k) {
                        if (writedata2[k] == readdata[k]) continue;

                        break;
                    }

                    if (k >= len) {
                        texttemp = "读取数据成功，数据为";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = BytestoHexString(readdata, ret);
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        sle4442tag = 1;

                        DisplayShow(totaltext, totallen);

                        tag |= 0x40;

                        return 0;
                    } else {
                        totallen = totallenbak;
                        System.arraycopy(totaltextbak, 0, totaltext, 0, totallen);
                    }
                }
            }
        }

        texttemp = "\n未寻找到副卡槽SLE4442卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int SLE4428CheckCard() {

        texttemp = "\n开始寻找副卡槽SLE4428卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        if (sle4428tag == 1) {
            return 0;
        } else if (sle4428tag == 0) {
            ret = sunmireader.sle4418TO5528Select();
            if (ret == 0) {
                byte [] key = HexStringtoBytes("FFFF");

                ret = sunmireader.sle4428And5528Auth(key);
                if (ret == 0) {
                    texttemp = "2. 寻找到接触式SLE4428卡\n";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    byte    len = 16;
                    byte [] readdata = new byte[len];

                    ret = sunmireader.sle4418TO5528ReadData(0x00, len, readdata);
                    if (ret < 0) {
                        texttemp = "读取数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x80;

                        return 0;
                    }

                    texttemp = "读取数据成功，数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = BytestoHexString(readdata, ret);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    data = "20170615112233445566778899000000";
                    rand = String.valueOf(Rand());

                    writedata = data.substring(0, data.length() - rand.length());
                    writedata += rand;
                    byte [] writedata2 = HexStringtoBytes(writedata);

                    texttemp = BytestoHexString(writedata2, writedata2.length);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    ret = sunmireader.sle4418TO5528WriteData(0x00, writedata2.length, writedata2);
                    if (ret < 0) {
                        texttemp = "，写入数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x80;

                        return 0;
                    }

                    texttemp = "，写入数据成功\n";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "开始读取数据：\n读取地址为0x00，";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    len = 16;

                    ret = sunmireader.sle4418TO5528ReadData(0x00, len, readdata);
                    if (ret < 0) {
                        texttemp = "读取数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x80;

                        return 0;
                    }

                    int k;

                    for (k = 0; k < len; ++k) {
                        if (writedata2[k] == readdata[k]) continue;

                        break;
                    }

                    if (k >= len) {
                        texttemp = "读取数据成功，数据为";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = BytestoHexString(readdata, ret);
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        sle4428tag = 1;

                        DisplayShow(totaltext, totallen);

                        tag |= 0x80;

                        return 0;
                    } else {
                        totallen = totallenbak;
                        System.arraycopy(totaltextbak, 0, totaltext, 0, totallen);
                    }
                }
            }
        }

        texttemp = "\n未寻找到副卡槽SLE4428卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int AT88SC102CheckCard() {

        texttemp = "\n开始寻找副卡槽AT88SC102卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        if (at88sc102tag == 1) {
            return 0;
        } else if (at88sc102tag == 0) {
            byte [] key = HexStringtoBytes("F0F0");

            ret = sunmireader.at88SC102Auth((byte) 0x0A, (byte) key.length, key);
            if (ret == 0) {
                texttemp = "2. 寻找到接触式AT88SC102卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x23，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte    len = 16;
                byte [] readdata = new byte[len];

                ret = sunmireader.at88SC102ReadData((byte) 0x23, len, readdata);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0100;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(readdata, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始写入数据：\n写入地址为0x23，写入数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                data = "20170615112233445566778899000000";
                rand = String.valueOf(Rand());

                writedata = data.substring(0, data.length() - rand.length());
                writedata += rand;
                byte [] writedata2 = HexStringtoBytes(writedata);

                texttemp = BytestoHexString(writedata2, writedata2.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.at88SC102WriteData((byte) 0x23, (byte) writedata2.length, writedata2);
                if (ret < 0) {
                    texttemp = "，写入数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0100;

                    return 0;
                }

                texttemp = "，写入数据成功\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取数据：\n读取地址为0x23，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                len = 16;

                ret = sunmireader.at88SC102ReadData((byte) 0x23, len, readdata);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0100;

                    return 0;
                }

                int k;

                for (k = 0; k < len; ++k) {
                    if (writedata2[k] == readdata[k]) continue;

                    break;
                }

                if (k >= len) {
                    texttemp = "读取数据成功，数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = BytestoHexString(readdata, ret);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    at88sc102tag = 1;

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0100;

                    return 0;
                } else {
                    totallen = totallenbak;
                    System.arraycopy(totaltextbak, 0, totaltext, 0, totallen);
                }
            }
        }

        texttemp = "\n未寻找到副卡槽AT88SC102卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int AT88SC1604CheckCard() {

        texttemp = "\n开始寻找副卡槽AT88SC1604卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        if (at88sc1604tag == 1) {
            return 0;
        } else if (at88sc1604tag == 0) {
            byte [] key = HexStringtoBytes("F0F0");

            ret = sunmireader.at88SC1604Auth(0x0A, key.length, key);
            if (ret == 0) {
                byte    len = 16;
                byte [] readdata = new byte[len];

                ret = sunmireader.at88SC1604ReadData(0x7F6, 2, readdata);
                if (ret == 0) {
                    texttemp = "2. 寻找到接触式AT88SC1604卡\n";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x27，";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    len = 16;

                    ret = sunmireader.at88SC1604ReadData(0x27, len, readdata);
                    if (ret < 0) {
                        texttemp = "读取数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x0200;

                        return 0;
                    }

                    texttemp = "读取数据成功，数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = BytestoHexString(readdata, ret);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "\n开始写入数据：\n写入地址为0x27，写入数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    data = "20170615112233445566778899000000";
                    rand = String.valueOf(Rand());

                    writedata = data.substring(0, data.length() - rand.length());
                    writedata += rand;
                    byte [] writedata2 = HexStringtoBytes(writedata);

                    texttemp = BytestoHexString(writedata2, writedata2.length);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    ret = sunmireader.at88SC1604WriteData(0x27, writedata2.length, writedata2);
                    if (ret < 0) {
                        texttemp = "，写入数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x0200;

                        return 0;
                    }

                    texttemp = "，写入数据成功\n";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = "开始读取数据：\n读取地址为0x27，";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    len = 16;

                    ret = sunmireader.at88SC1604ReadData(0x27, len, readdata);
                    if (ret < 0) {
                        texttemp = "读取数据失败";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        DisplayShow(totaltext, totallen);

                        tag |= 0x0200;

                        return 0;
                    }

                    int k;

                    for (k = 0; k < len; ++k) {
                        if (writedata2[k] == readdata[k]) continue;

                        break;
                    }

                    if (k >= len) {
                        texttemp = "读取数据成功，数据为";
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        texttemp = BytestoHexString(readdata, ret);
                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                        totallen += texttemp.length();

                        at88sc1604tag = 1;

                        DisplayShow(totaltext, totallen);

                        tag |= 0x0200;

                        return 0;
                    } else {
                        totallen = totallenbak;
                        System.arraycopy(totaltextbak, 0, totaltext, 0, totallen);
                    }
                }
            }
        }

        texttemp = "\n未寻找到副卡槽AT88SC1604卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int AT45DBCheckCard() {

        texttemp = "\n开始寻找副卡槽AT45DB系列卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        if (at45dbtag == 1) {
            return 0;
        } else if (at45dbtag == 0) {
            ret = sunmireader.at45DBSelect();
            if (ret == 0) {
                texttemp = "2. 寻找到接触式AT45DB卡\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                byte    len = 16;
                byte [] readdata = new byte[len];

                ret = sunmireader.at45DBReadData(0x00, len, readdata);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0400;

                    return 0;
                }

                texttemp = "读取数据成功，数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = BytestoHexString(readdata, ret);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                data = "20170615112233445566778899000000";
                rand = String.valueOf(Rand());

                writedata = data.substring(0, data.length() - rand.length());
                writedata += rand;
                byte [] writedata2 = HexStringtoBytes(writedata);

                texttemp = BytestoHexString(writedata2, writedata2.length);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                ret = sunmireader.at45DBWriteData(0x00, writedata2.length, writedata2);
                if (ret < 0) {
                    texttemp = "，写入数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0400;

                    return 0;
                }

                texttemp = "，写入数据成功\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "开始读取数据：\n读取地址为0x00，";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                len = 16;

                ret = sunmireader.at45DBReadData(0x00, len, readdata);
                if (ret < 0) {
                    texttemp = "读取数据失败";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0400;

                    return 0;
                }

                int k;

                for (k = 0; k < len; ++k) {
                    if (writedata2[k] == readdata[k]) continue;

                    break;
                }

                if (k >= len) {
                    texttemp = "读取数据成功，数据为";
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    texttemp = BytestoHexString(writedata2, ret);
                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                    totallen += texttemp.length();

                    at45dbtag = 1;

                    DisplayShow(totaltext, totallen);

                    tag |= 0x0400;

                    return 0;
                } else {
                    totallen = totallenbak;
                    System.arraycopy(totaltextbak, 0, totaltext, 0, totallen);
                }
            }
        }

        texttemp = "\n未寻找到副卡槽AT45DB系列卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int AT24CXXCheckCard()  {

        int    k;
        int    ret2,ret3,ret4,ret5,ret6;
        byte   len = 16;
        byte[] at24C32to1024ReadDataRsp = new byte[len];
        byte[] at24C32to1024ReadDataRsp2 = new byte[len];
        byte[] at24C01TO16ReadDataRsp = new byte[len];
        byte[] at24C01TO16ReadDataRsp2 = new byte[len];
        byte[] writedata2 = new byte[2];

        Date dt1 = new Date();
        Long time1 = dt1.getTime();

        texttemp = "\n开始寻找副卡槽AT24C系列卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        if (at24cxxtag == 1) {
            return 0;
        } else if (at24cxxtag == 0) {
            ret = sunmireader.at24C01to16Select();
            if (ret == 0) {
                ret = sunmireader.at24C01to16ReadData(0x00, 1, at24C01TO16ReadDataRsp);
                if (ret >= 0) {
                    byte [] tempdata = HexStringtoBytes("63");

                    ret = sunmireader.at24C01to16WriteData(0x00, 1, tempdata);
                    if (ret == 0) {
                        byte[] databak = new byte[2];
                        databak[0] = at24C01TO16ReadDataRsp[0];

                        ret = sunmireader.at24C01to16ReadData(0x00, 1, at24C01TO16ReadDataRsp2);
                        if (ret >= 0) {
                            if (at24C01TO16ReadDataRsp2[0] == tempdata[0]) {
                                System.arraycopy(databak, 0, tempdata, 0, 1);

                                sunmireader.at24C01to16WriteData(0x00, 1, tempdata);

                                ret2 = sunmireader.at24C01to16ReadData(0x07FE, 2, at24C01TO16ReadDataRsp);
                                {
                                    ret3 = sunmireader.at24C01to16ReadData(0x06FE, 2, at24C01TO16ReadDataRsp2);
                                    {
                                        byte[] writedatabak = new byte[2];

                                        tempdata = HexStringtoBytes("6677");

                                        System.arraycopy(tempdata, 0, writedatabak, 0, tempdata.length);

                                        ret4 = sunmireader.at24C01to16WriteData(0x07FE, 2, tempdata);
                                        {
                                            byte[] databak2 = new byte[2];

                                            databak2[0] = at24C01TO16ReadDataRsp[0];
                                            databak2[1] = at24C01TO16ReadDataRsp[1];

                                            tempdata = HexStringtoBytes("8899");

                                            ret5 = sunmireader.at24C01to16WriteData(0x06FE, 2, tempdata);
                                            {
                                                ret6 = sunmireader.at24C01to16ReadData(0x07FE, 2, at24C01TO16ReadDataRsp);
                                                {
                                                    k = 0;

                                                    if (ret2 >= 0 && ret3 >= 0 && ret4 >= 0 && ret5 >= 0 && ret6 >= 0) {
                                                        for (k = 0; k < 2; ++k) {
                                                            if (writedatabak[k] == at24C01TO16ReadDataRsp[k]) continue;

                                                            break;
                                                        }

                                                        System.arraycopy(databak2, 0, tempdata, 0, 2);

                                                        sunmireader.at24C01to16WriteData(0x07FE, 2, tempdata);

                                                        System.arraycopy(at24C01TO16ReadDataRsp2, 0, tempdata, 0, 2);

                                                        sunmireader.at24C01to16WriteData(0x06FE, 2, tempdata);
                                                    }

                                                    System.arraycopy(at24C01TO16ReadDataRsp2, 0, writedata2, 0, 2);

                                                    sunmireader.at24C01to16WriteData(0x06FE, 2, writedata2);

                                                    tempdata = HexStringtoBytes(databak2.toString());

                                                    sunmireader.at24C01to16WriteData(0x07FE, 2, tempdata);

                                                    if (k >= 2) {
                                                        texttemp = "2. 寻找到接触式AT24C16卡\n";
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        len = 16;

                                                        ret = sunmireader.at24C01to16ReadData(0x00, len, at24C01TO16ReadDataRsp2);
                                                        if (ret < 0) {
                                                            texttemp = "读取数据失败";
                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                            totallen += texttemp.length();

                                                            DisplayShow(totaltext, totallen);

                                                            tag |= 0x0800;

                                                            return 0;
                                                        }

                                                        texttemp = "读取数据成功，数据为";
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp2, ret);
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        len = 16;

                                                        data = "20170615112233445566778899000000";
                                                        rand = String.valueOf(Rand());

                                                        writedata = data.substring(0, data.length() - rand.length());
                                                        writedata += rand;
                                                        tempdata = HexStringtoBytes(writedata);

                                                        texttemp = BytestoHexString(tempdata, tempdata.length);
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        ret = sunmireader.at24C01to16WriteData(0x00, len, tempdata);
                                                        if (ret < 0) {
                                                            texttemp = "，写入数据失败";
                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                            totallen += texttemp.length();

                                                            DisplayShow(totaltext, totallen);

                                                            tag |= 0x0800;

                                                            return 0;
                                                        }

                                                        texttemp = "，写入数据成功\n";
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        texttemp = "开始读取数据：\n读取地址为0x00，";
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        len = 16;

                                                        ret = sunmireader.at24C01to16ReadData(0x00, len, at24C01TO16ReadDataRsp);
                                                        if (ret < 0) {
                                                            texttemp = "读取数据失败";
                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                            totallen += texttemp.length();

                                                            DisplayShow(totaltext, totallen);

                                                            tag |= 0x0800;

                                                            return 0;
                                                        }

                                                        texttemp = "读取数据成功，数据为";
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp, ret);
                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                        totallen += texttemp.length();

                                                        at24cxxtag = 1;

                                                        DisplayShow(totaltext, totallen);

                                                        Date dt2 = new Date();
                                                        Long time2 = dt2.getTime();
                                                        String temp = String.valueOf(time2 - time1);
                                                        Log.v("AT24C16", temp);

                                                        tag |= 0x0800;

                                                        return 0;
                                                    } else {
                                                        ret2 = sunmireader.at24C01to16ReadData(0x03FE, 2, at24C01TO16ReadDataRsp);
                                                        {
                                                            ret3 = sunmireader.at24C01to16ReadData(0x02FE, 2, at24C01TO16ReadDataRsp2);
                                                            {
                                                                tempdata = HexStringtoBytes("6677");

                                                                System.arraycopy(tempdata, 0, writedatabak, 0, 2);

                                                                ret4 = sunmireader.at24C01to16WriteData(0x03FE, 2, tempdata);
                                                                {
                                                                    databak2[0] = at24C01TO16ReadDataRsp[0];
                                                                    databak2[1] = at24C01TO16ReadDataRsp[1];

                                                                    tempdata = HexStringtoBytes("8899");

                                                                    ret5 = sunmireader.at24C01to16WriteData(0x02FE, 2, tempdata);
                                                                    {
                                                                        ret6 = sunmireader.at24C01to16ReadData(0x03FE, 2, at24C01TO16ReadDataRsp);
                                                                        {
                                                                            k = 0;

                                                                            if (ret2 >= 0 && ret3 >= 0 && ret4 >= 0 && ret5 >= 0 && ret6 >= 0) {
                                                                                for (k = 0; k < 2; ++k) {
                                                                                    if (writedatabak[k] == at24C01TO16ReadDataRsp[k]) continue;

                                                                                    break;
                                                                                }

                                                                                System.arraycopy(databak2, 0, tempdata, 0, 2);

                                                                                sunmireader.at24C01to16WriteData(0x03FE, 2, tempdata);

                                                                                System.arraycopy(at24C01TO16ReadDataRsp2, 0, tempdata, 0, 2);

                                                                                sunmireader.at24C01to16WriteData(0x02FE, 2, tempdata);
                                                                            }

                                                                            if (k >= 2) {
                                                                                texttemp = "2. 寻找到接触式AT24C08卡\n";
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp2);
                                                                                if (ret < 0) {
                                                                                    texttemp = "读取数据失败";
                                                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                    totallen += texttemp.length();

                                                                                    DisplayShow(totaltext, totallen);

                                                                                    tag |= 0x0800;

                                                                                    return 0;
                                                                                }

                                                                                texttemp = "读取数据成功，数据为";
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                texttemp = BytestoHexString(at24C01TO16ReadDataRsp2, ret);
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                data = "20170615112233445566778899000000";
                                                                                rand = String.valueOf(Rand());

                                                                                writedata = data.substring(0, data.length() - rand.length());
                                                                                writedata += rand;
                                                                                tempdata = HexStringtoBytes(writedata);

                                                                                texttemp = BytestoHexString(tempdata, tempdata.length);
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                ret = sunmireader.at24C01to16WriteData(0x00, 16, tempdata);
                                                                                if (ret < 0) {
                                                                                    texttemp = "，写入数据失败";
                                                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                    totallen += texttemp.length();

                                                                                    DisplayShow(totaltext, totallen);

                                                                                    tag |= 0x0800;

                                                                                    return 0;
                                                                                }

                                                                                texttemp = "，写入数据成功\n";
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                texttemp = "开始读取数据：\n读取地址为0x00，";
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp);
                                                                                if (ret < 0) {
                                                                                    texttemp = "读取数据失败";
                                                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                    totallen += texttemp.length();

                                                                                    DisplayShow(totaltext, totallen);

                                                                                    tag |= 0x0800;

                                                                                    return 0;
                                                                                }

                                                                                texttemp = "读取数据成功，数据为";
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                texttemp = BytestoHexString(at24C01TO16ReadDataRsp, ret);
                                                                                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                totallen += texttemp.length();

                                                                                at24cxxtag = 1;

                                                                                DisplayShow(totaltext, totallen);

                                                                                Date dt2 = new Date();
                                                                                Long time2 = dt2.getTime();
                                                                                String temp = String.valueOf(time2 - time1);
                                                                                Log.v("AT24C08", temp);

                                                                                tag |= 0x0800;

                                                                                return 0;
                                                                            } else {
                                                                                ret2 = sunmireader.at24C01to16ReadData(0x01FE, 2, at24C01TO16ReadDataRsp);
                                                                                {
                                                                                    ret3 = sunmireader.at24C01to16ReadData(0xFE, 2, at24C01TO16ReadDataRsp2);
                                                                                    {
                                                                                        tempdata = HexStringtoBytes("6677");

                                                                                        System.arraycopy(tempdata, 0, writedatabak, 0, 2);

                                                                                        ret4 = sunmireader.at24C01to16WriteData(0x01FE, 2, tempdata);
                                                                                        {
                                                                                            tempdata = HexStringtoBytes("8899");

                                                                                            ret5 = sunmireader.at24C01to16WriteData(0xFE, 2, tempdata);
                                                                                            {
                                                                                                databak2[0] = at24C01TO16ReadDataRsp[0];
                                                                                                databak2[1] = at24C01TO16ReadDataRsp[1];

                                                                                                ret6 = sunmireader.at24C01to16ReadData(0x01FE, 2, at24C01TO16ReadDataRsp);
                                                                                                {
                                                                                                    k = 0;

                                                                                                    if (ret2 >= 0 && ret3 >= 0 && ret4 >= 0 && ret5 >= 0 && ret6 >= 0) {
                                                                                                        for (k = 0; k < 2; ++k) {
                                                                                                            if (writedatabak[k] == at24C01TO16ReadDataRsp[k]) continue;

                                                                                                            break;
                                                                                                        }

                                                                                                        System.arraycopy(databak2, 0, tempdata, 0, 2);

                                                                                                        sunmireader.at24C01to16WriteData(0x01FE, 2, tempdata);

                                                                                                        System.arraycopy(at24C01TO16ReadDataRsp2, 0, tempdata, 0, 2);

                                                                                                        sunmireader.at24C01to16WriteData(0xFE, 2, tempdata);
                                                                                                    }

                                                                                                    if (k >= 2) {
                                                                                                        texttemp = "2. 寻找到接触式AT24C04卡\n";
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp2);
                                                                                                        if (ret < 0) {
                                                                                                            texttemp = "读取数据失败";
                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                            totallen += texttemp.length();

                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                            tag |= 0x0800;

                                                                                                            return 0;
                                                                                                        }

                                                                                                        texttemp = "读取数据成功，数据为";
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp2, ret);
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        data = "20170615112233445566778899000000";
                                                                                                        rand = String.valueOf(Rand());

                                                                                                        writedata = data.substring(0, data.length() - rand.length());
                                                                                                        writedata += rand;
                                                                                                        tempdata = HexStringtoBytes(writedata);

                                                                                                        texttemp = BytestoHexString(tempdata, tempdata.length);
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        ret = sunmireader.at24C01to16WriteData(0x00, 16, tempdata);
                                                                                                        if (ret < 0) {
                                                                                                            texttemp = "，写入数据失败";
                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                            totallen += texttemp.length();

                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                            tag |= 0x0800;

                                                                                                            return 0;
                                                                                                        }

                                                                                                        texttemp = "，写入数据成功\n";
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        texttemp = "开始读取数据：\n读取地址为0x00，";
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp);
                                                                                                        if (ret < 0) {
                                                                                                            texttemp = "读取数据失败";
                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                            totallen += texttemp.length();

                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                            tag |= 0x0800;

                                                                                                            return 0;
                                                                                                        }

                                                                                                        texttemp = "读取数据成功，数据为";
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp, ret);
                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                        totallen += texttemp.length();

                                                                                                        at24cxxtag = 1;

                                                                                                        DisplayShow(totaltext, totallen);

                                                                                                        Date dt2 = new Date();
                                                                                                        Long time2 = dt2.getTime();
                                                                                                        String temp = String.valueOf(time2 - time1);
                                                                                                        Log.v("AT24C04", temp);

                                                                                                        tag |= 0x0800;

                                                                                                        return 0;
                                                                                                    } else {
                                                                                                        ret2 = sunmireader.at24C01to16ReadData(0x80, 2, at24C01TO16ReadDataRsp2);
                                                                                                        {
                                                                                                            tempdata = HexStringtoBytes("5750");

                                                                                                            ret3 = sunmireader.at24C01to16WriteData(0x80, 2, tempdata);
                                                                                                            {
                                                                                                                databak2[0] = at24C01TO16ReadDataRsp2[0];
                                                                                                                databak2[1] = at24C01TO16ReadDataRsp2[1];

                                                                                                                ret4 = sunmireader.at24C01to16ReadData(0x80, 2, at24C01TO16ReadDataRsp);
                                                                                                                {
                                                                                                                    k = 0;

                                                                                                                    if (ret2 >= 0 && ret3 >= 0 && ret4 >= 0) {
                                                                                                                        for (k = 0; k < 2; ++k) {
                                                                                                                            if (tempdata[k] == at24C01TO16ReadDataRsp[k]) continue;

                                                                                                                            break;
                                                                                                                        }

                                                                                                                        System.arraycopy(at24C01TO16ReadDataRsp2, 0, tempdata, 0, 2);

                                                                                                                        sunmireader.at24C01to16WriteData(0x80, 2, tempdata);
                                                                                                                    }

                                                                                                                    if (k >= 2) {
                                                                                                                        texttemp = "2. 寻找到接触式AT24C02卡\n";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp2);
                                                                                                                        if (ret < 0) {
                                                                                                                            texttemp = "读取数据失败";
                                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                            totallen += texttemp.length();

                                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                                            tag |= 0x0800;

                                                                                                                            return 0;
                                                                                                                        }

                                                                                                                        texttemp = "读取数据成功，数据为";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp2, ret);
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        data = "20170615112233445566778899000000";
                                                                                                                        rand = String.valueOf(Rand());

                                                                                                                        writedata = data.substring(0, data.length() - rand.length());
                                                                                                                        writedata += rand;
                                                                                                                        tempdata = HexStringtoBytes(writedata);

                                                                                                                        texttemp = BytestoHexString(tempdata, tempdata.length);
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        ret = sunmireader.at24C01to16WriteData(0x00, 16, tempdata);
                                                                                                                        if (ret < 0) {
                                                                                                                            texttemp = "，写入数据失败";
                                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                            totallen += texttemp.length();

                                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                                            tag |= 0x0800;

                                                                                                                            return 0;
                                                                                                                        }

                                                                                                                        texttemp = "，写入数据成功\n";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = "开始读取数据：\n读取地址为0x00，";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp);
                                                                                                                        if (ret < 0) {
                                                                                                                            texttemp = "读取数据失败";
                                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                            totallen += texttemp.length();

                                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                                            tag |= 0x0800;

                                                                                                                            return 0;
                                                                                                                        }

                                                                                                                        texttemp = "读取数据成功，数据为";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp, ret);
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        at24cxxtag = 1;

                                                                                                                        DisplayShow(totaltext, totallen);

                                                                                                                        Date dt2 = new Date();
                                                                                                                        Long time2 = dt2.getTime();
                                                                                                                        String temp = String.valueOf(time2 - time1);
                                                                                                                        Log.v("AT24C02", temp);

                                                                                                                        tag |= 0x0800;

                                                                                                                        return 0;
                                                                                                                    } else {
                                                                                                                        texttemp = "2. 寻找到接触式AT24C01卡\n";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp2);
                                                                                                                        if (ret < 0) {
                                                                                                                            texttemp = "读取数据失败";
                                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                            totallen += texttemp.length();

                                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                                            tag |= 0x0800;

                                                                                                                            return 0;
                                                                                                                        }

                                                                                                                        texttemp = "读取数据成功，数据为";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp2, ret);
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        data = "20170615112233445566778899000000";
                                                                                                                        rand = String.valueOf(Rand());

                                                                                                                        writedata = data.substring(0, data.length() - rand.length());
                                                                                                                        writedata += rand;
                                                                                                                        tempdata = HexStringtoBytes(writedata);

                                                                                                                        texttemp = BytestoHexString(tempdata, tempdata.length);
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        ret = sunmireader.at24C01to16WriteData(0x00, 16, tempdata);
                                                                                                                        if (ret < 0) {
                                                                                                                            texttemp = "，写入数据失败";
                                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                            totallen += texttemp.length();

                                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                                            tag |= 0x0800;

                                                                                                                            return 0;
                                                                                                                        }

                                                                                                                        texttemp = "，写入数据成功\n";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = "开始读取数据：\n读取地址为0x00，";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        ret = sunmireader.at24C01to16ReadData(0x00, 16, at24C01TO16ReadDataRsp);
                                                                                                                        if (ret < 0) {
                                                                                                                            texttemp = "读取数据失败";
                                                                                                                            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                            totallen += texttemp.length();

                                                                                                                            DisplayShow(totaltext, totallen);

                                                                                                                            tag |= 0x0800;

                                                                                                                            return 0;
                                                                                                                        }

                                                                                                                        texttemp = "读取数据成功，数据为";
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        texttemp = BytestoHexString(at24C01TO16ReadDataRsp, ret);
                                                                                                                        System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                                                                                        totallen += texttemp.length();

                                                                                                                        at24cxxtag = 1;

                                                                                                                        DisplayShow(totaltext, totallen);

                                                                                                                        Date dt2 = new Date();
                                                                                                                        Long time2 = dt2.getTime();
                                                                                                                        String temp = String.valueOf(time2 - time1);
                                                                                                                        Log.v("AT24C01", temp);

                                                                                                                        tag |= 0x0800;

                                                                                                                        return 0;
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            } else {
                                System.arraycopy(databak, 0, tempdata, 0, 1);

                                sunmireader.at24C01to16WriteData(0x00, 1, tempdata);

                                ret = sunmireader.at24C32to1024Select();
                                if (ret == 0) {
                                    ret = sunmireader.at24C32to1024ReadData(0x00, 16, at24C32to1024ReadDataRsp);
                                    if (ret >= 0) {
                                        data = "20170615112233445566778899000000";
                                        rand = String.valueOf(Rand());

                                        writedata = data.substring(0, data.length() - rand.length());
                                        writedata += rand;
                                        tempdata = HexStringtoBytes(writedata);

                                        ret = sunmireader.at24C32to1024WriteData(0x00, 16, tempdata);
                                        if (ret == 0) {
                                            ret = sunmireader.at24C32to1024ReadData(0x00, 16, at24C32to1024ReadDataRsp2);
                                            if (ret >= 0) {
                                                for (k = 0; k < 16; ++k) {
                                                    if (tempdata[k] == at24C32to1024ReadDataRsp2[k]) continue;

                                                    break;
                                                }

                                                if (k >= 16) {
                                                    texttemp = "2. 寻找到接触式AT24C64卡\n";
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = "3. 开始操作卡片：\n开始读取数据：\n读取地址为0x00，";
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = "读取数据成功，数据为";
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = BytestoHexString(at24C32to1024ReadDataRsp, 16);
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = "\n开始写入数据：\n写入地址为0x00，写入数据为";
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = BytestoHexString(tempdata, 16);
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = "，写入数据成功\n";
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = "开始读取数据：\n读取地址为0x00，";
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = "读取数据成功，数据为";
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    texttemp = BytestoHexString(at24C32to1024ReadDataRsp2, 16);
                                                    System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                                                    totallen += texttemp.length();

                                                    at24cxxtag = 1;

                                                    DisplayShow(totaltext, totallen);

                                                    Date dt2 = new Date();
                                                    Long time2 = dt2.getTime();
                                                    String temp = String.valueOf(time2 - time1);
                                                    Log.v("AT24C64", temp);

                                                    tag |= 0x0800;

                                                    return 0;
                                                } else {
                                                    byte[] writedata = new byte[16];

                                                    System.arraycopy(at24C32to1024ReadDataRsp, 0, writedata, 0, 16);

                                                    sunmireader.at24C32to1024WriteData(0x00, 16, writedata);
                                                }
                                            } else {
                                                byte[] writedata = new byte[16];

                                                System.arraycopy(at24C32to1024ReadDataRsp, 0, writedata, 0, 16);

                                                sunmireader.at24C32to1024WriteData(0x00, 16, writedata);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        System.arraycopy(databak, 0, tempdata, 0, 1);

                        sunmireader.at24C01to16WriteData(0x00, 1, tempdata);
                    }
                }
            }
        }

        texttemp = "\n未寻找到副卡槽AT24C系列卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        return -1;
    }

    public int StripCheckCard() {

        texttemp = "\n开始寻找磁条卡";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);

        TrackData trackdata = new TrackData();
        trackdata.trackdata1 = new byte[79];
        trackdata.trackdata2 = new byte[40];
        trackdata.trackdata3 = new byte[107];

        byte [] data = new byte[110];

        stripcheckcardoutime = 5;
        stripcheckcardoutimetag = 1;

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (stripcheckcardoutimetag == 1) {
                        for (int i = stripcheckcardoutime; i > 0; --i) {
                            if (i > 0) mHandler.sendEmptyMessage(i);

                            if (stripcheckcardoutimetag == 0) break;

                            Thread.sleep(1000);

                            if (stripcheckcardoutimetag == 0) break;
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        ret = sunmireader.magneticStripCardReadData(stripcheckcardoutime, trackdata);

        stripcheckcardoutimetag = 0;

        if (ret == 0) {
            sunmireader.beepTime((char) 100);

            texttemp = "2. 寻找到磁条卡\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            texttemp = "3. 开始操作卡片：\n";
            System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
            totallen += texttemp.length();

            if (trackdata.len1 > 0) {
                texttemp = "磁道1数据：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                System.arraycopy(trackdata.trackdata1, 0, data, 0, trackdata.len1);

                texttemp = BytestoHexString(data, trackdata.len1);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            }

            if (trackdata.len2 > 0) {
                texttemp = "磁道2数据：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                System.arraycopy(trackdata.trackdata2, 0, data, 0, trackdata.len2);

                texttemp = BytestoHexString(data, trackdata.len2);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            }

            if (trackdata.len3 > 0) {
                texttemp = "磁道3数据：";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                System.arraycopy(trackdata.trackdata3, 0, data, 0, trackdata.len3);

                texttemp = BytestoHexString(data, trackdata.len3);
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();

                texttemp = "\n";
                System.arraycopy(texttemp.toCharArray(), 0, totaltext, totallen, texttemp.length());
                totallen += texttemp.length();
            }

            DisplayShow(totaltext, totallen);

            tag |= 0x1000;

            return 0;
        }

        tag &= 0xEFFF;

        texttemp = "\n未寻找到磁条卡\n";
        System.arraycopy(texttemp.toCharArray(), 0, totaltextlog, totallenlog, texttemp.length());
        totallenlog += texttemp.length();

        DisplayShow(totaltextlog, totallenlog);


        return -1;
    }

    public int CheckCardExist() {

        byte index = 0x00;

        ret = sunmireader.cardSlotCheck(index);
        if (ret < 0) {
            sle4442tag = 0;
            sle4428tag = 0;
            at24cxxtag = 0;
            at88sc102tag = 0;
            at88sc1604tag = 0;
            at45dbtag = 0;

            tag &= 0xFFDF;
            tag &= 0xFFBF;
            tag &= 0xFF7F;
            tag &= 0xFEFF;
            tag &= 0xFDFF;
            tag &= 0xFBFF;
            tag &= 0xF7FF;

            return -1;
        }

        if (sle4442tag == 1) return 1;
        else if (sle4428tag == 1) return 1;
        else if (at24cxxtag == 1) return 1;
        else if (at88sc102tag == 1) return 1;
        else if (at88sc1604tag == 1) return 1;
        else if (at45dbtag == 1) return 1;

//        byte [] atr = new byte[255];
//
//        sunmireader.cardSlotCtrl(index, (byte) 0x00, (byte) 0x01, atr);

        return 0;
    }

    public void DisplayShow(char[] text, int len) {

        if (text == null) return;
        if (len <= 0) return;

        final int l = len;
        final char [] temp = new char[l+3];

        if (tag == 0) {
            System.arraycopy(text, 0, temp, 0, l);
            temp[l] = '\n';
            temp[l+1] = '\n';
            temp[l+2] = '\n';

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    scanf_card_info_scrollview.fullScroll(ScrollView.FOCUS_DOWN);
                    scanf_card_info_textview.setText(temp, 0, l+3);
                }
            });
        }
    }

    public static final String BytestoASCII(byte[] data, int len) {
        int i;
        int j;
        StringBuffer tStringBuf = new StringBuffer();

        char[] tChars = new char[len];

        for (i = 0; i < len; i++) {
            tChars[i] = (char) data[i];
        }

        tStringBuf.append(tChars);

        return tStringBuf.toString();
    }

    public static final String BytestoHexString(byte[] bArray, int len) {
        StringBuffer sb = new StringBuffer(len);
        String sTemp;

        for (int i = 0; i < len; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) sb.append(0);
            sb.append(sTemp.toUpperCase());
        }

        return sb.toString();
    }

    public static final byte[] HexStringtoBytes(String hexString) {
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static final int Rand() {
        Random random = new Random();

        return random.nextInt();
    }

}
